#lang racket
(require math/number-theory)


(define (trim-left-0 s)
  (string-trim s "0" #:left? #t #:right? #f  #:repeat? #t))

(define (du-str s)
  (let ([s (trim-left-0 s)])
    (if (string=? s "") ""
        (cond
          [(string=? s "0") "零"]
          [(string=? s "1") "壹"]
          [(string=? s "2") "贰"]
          [(string=? s "3") "叁"]
          [(string=? s "4") "肆"]
          [(string=? s "5") "伍"]
          [(string=? s "6") "陆"]
          [(string=? s "7") "柒"]
          [(string=? s "8") "捌"]
          [(string=? s "9") "玖"]
          [else
           (let* [[L (string-length s)]
                  [L1  (cond
                         [(= L 2 ) '(1 "拾")]
                         [(= L 3 ) '(1 "佰")]
                         [(= L 4 ) '(1 "仟")]
                         [(= L 5 ) '(1 "万")]
                         [(= L 6 ) '(2 "万")]
                         [(= L 7 ) '(3 "万")]
                         [(= L 8 ) '(4 "万")]
                         [else
                          (let* ([r (remainder L 8)]
                                 [q (quotient L 8)]
                                 [q* (if (= r 0) (- q 1) q)])
                            `(,(- L (* 8  q*))
                              ,(make-string q*  #\亿))
                            )]
                         )]
                  [head-len (car L1)]
                  [head (substring s 0 head-len)]
                  [tail-len (- L head-len)]
                  [tail (substring s head-len)]
                  [tail-len-trimed  (string-length (trim-left-0 tail))]
                  [unit (cadr L1)]
                  ]
             (string-append (du-str head)
                            unit
                            (if (and  (> tail-len-trimed 0) (> (- tail-len tail-len-trimed) 0))  "零" "")
                            (du-str tail))
             )]))))

 ;;(du-str "12345678000000001234567800000000005437549305784327509840000000")

